package com.linx.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linx.common.core.domain.dto.RoleDTO;
import com.linx.common.core.domain.model.LoginUser;
import com.linx.common.core.utils.MapstructUtils;
import com.linx.common.mybatis.core.page.PageQuery;
import com.linx.common.mybatis.core.page.TableDataInfo;
import com.linx.common.satoken.utils.LoginHelper;
import com.linx.system.domain.AppUserCommonlyUsedMenu;
import com.linx.system.domain.bo.AppUserCommonlyUsedMenuBo;
import com.linx.system.domain.vo.AppUserCommonlyUsedMenuVo;
import com.linx.system.mapper.AppUserCommonlyUsedMenuMapper;
import com.linx.system.service.IAppUserCommonlyUsedMenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 移动端常用菜单Service业务层处理
 *
 * @author linx
 * @date 2024-12-25
 */
@RequiredArgsConstructor
@Service
public class AppUserCommonlyUsedMenuServiceImpl implements IAppUserCommonlyUsedMenuService {

    private final AppUserCommonlyUsedMenuMapper baseMapper;

    /**
     * 查询移动端常用菜单
     *
     * @param appUserCommonlyUsedMenuId 主键
     * @return 移动端常用菜单
     */
    @Override
    public AppUserCommonlyUsedMenuVo queryById(Long appUserCommonlyUsedMenuId) {
        return baseMapper.selectVoById(appUserCommonlyUsedMenuId);
    }

    /**
     * 分页查询移动端常用菜单列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 移动端常用菜单分页列表
     */
    @Override
    public TableDataInfo<AppUserCommonlyUsedMenuVo> queryPageList(AppUserCommonlyUsedMenuBo bo, PageQuery pageQuery) {
        //LambdaQueryWrapper<AppUserCommonlyUsedMenu> lqw = buildQueryWrapper(bo);
        //Page<AppUserCommonlyUsedMenuVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        //return TableDataInfo.build(result);
        LoginUser loginUser = LoginHelper.getLoginUser();
        Long userId = LoginHelper.getUserId();
        bo.setUserId(userId);

        List<RoleDTO> roles = LoginHelper.getLoginUser().getRoles();
        List<Long> roleIdList= new ArrayList<>();
        if (CollUtil.isNotEmpty(roles)) {
            //从roles中获取roleId的集合
            roleIdList = roles.stream()
                .map(RoleDTO::getRoleId)
                .collect(Collectors.toList());

        }
        bo.setRoleIds(roleIdList);
        Page<AppUserCommonlyUsedMenuVo> result = baseMapper.selectVoPageNew(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的移动端常用菜单列表
     *
     * @param bo 查询条件
     * @return 移动端常用菜单列表
     */
    @Override
    public List<AppUserCommonlyUsedMenuVo> queryList(AppUserCommonlyUsedMenuBo bo) {
        LambdaQueryWrapper<AppUserCommonlyUsedMenu> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppUserCommonlyUsedMenu> buildQueryWrapper(AppUserCommonlyUsedMenuBo bo) {
        LambdaQueryWrapper<AppUserCommonlyUsedMenu> lqw = Wrappers.lambdaQuery();
        return lqw;
    }

    /**
     * 新增移动端常用菜单
     *
     * @param bo 移动端常用菜单
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(AppUserCommonlyUsedMenuBo bo) {
        Long userId = LoginHelper.getUserId();
        bo.setUserId(userId);
        AppUserCommonlyUsedMenu add = MapstructUtils.convert(bo, AppUserCommonlyUsedMenu.class);
        validEntityBeforeSave(add);

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAppUserCommonlyUsedMenuId(add.getAppUserCommonlyUsedMenuId());
        }
        return flag;
    }

    @Override
    public Boolean addOrUpdate(List<AppUserCommonlyUsedMenuBo> list) {
        //根据userId删除出所有的常用菜单
        Long userId = LoginHelper.getUserId();
        baseMapper.delete(Wrappers.<AppUserCommonlyUsedMenu>lambdaQuery().eq(AppUserCommonlyUsedMenu::getUserId,LoginHelper.getUserId()));
        List<AppUserCommonlyUsedMenu> add = MapstructUtils.convert(list, AppUserCommonlyUsedMenu.class);
        for(int i = 0; i < add.size(); i++){
            add.get(i).setUserId(userId);
            add.get(i).setCreateBy(userId);
            add.get(i).setSort(Long.valueOf(i+1));
        };
        boolean flag = baseMapper.insertBatch(add);

        return flag;
    }

    /**
     * 修改移动端常用菜单
     *
     * @param bo 移动端常用菜单
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(AppUserCommonlyUsedMenuBo bo) {
        Long userId = LoginHelper.getUserId();
        bo.setUserId(userId);
        AppUserCommonlyUsedMenu update = MapstructUtils.convert(bo, AppUserCommonlyUsedMenu.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppUserCommonlyUsedMenu entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除移动端常用菜单信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
